home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Scene Storm
/
Scene Storm - Volume 1.iso
/
coding
/
c
/
northc
/
example2.lzh
/
make
/
file.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-08-30
|
7KB
|
333 lines
/*
(c) 1990 S.Hawtin.
Permission is granted to copy this file provided that:
1) It is not used for commercial gain
2) This notice is included in all copies
3) Altered copies are marked as such.
No liability is accepted for the contents of the file.
file.c within WBmake
*/
/* Handle the file and variable data types */
#include <stdio.h>
#include <string.h>
#include <libraries/dos.h>
#include "make.h"
#define MAX_HASH 128
extern ConsCell *defaults;
static ConsCell *files[MAX_HASH];
static Variable *vars[MAX_HASH];
/* The file info block must be word alligned */
static long padding;
static struct FileInfoBlock infodata;
static int
hash(str)
char *str;
{/* Simple hashing */
int i;
for(i=0;*str;str++)
i += *str;
return(i % MAX_HASH);
}
long
type_of(str)
char *str;
{/* Clasify the file type */
char *temp;
int i;
long ret;
temp = strrchr(str,'.');
ret = 0;
if(temp!=NULL)
{/* Only first three bytes of extension are used */
temp++;
for(i=0;i<4 && temp[i]!='\0';i++)
ret = (ret<<8)+temp[i];
return(ret);
}
return(EXE_FILE);
}
ConsCell *
cons(the_car,the_cdr)
FileInfo *the_car;
ConsCell *the_cdr;
{/* Create a list element */
ConsCell *this;
this = (ConsCell *)calloc(sizeof(ConsCell),1);
if(this==NULL)
{printf("Out of memory\n");
exit(10);
}
this->car = the_car;
this->cdr = the_cdr;
return(this);
}
long
dstodate(ds)
struct DateStamp *ds;
{/* Convert a datestanp to a date */
long ret;
ret = ds->ds_Days;
ret = ret*24*60 + ds->ds_Minute;
ret = ret*60 + (ds->ds_Tick/TICKS_PER_SECOND);
return(ret);
}
static char *
get_addr(addr)
unsigned long addr;
{/* Chop the bottom two bits from the address to ensure long
word allignment */
return(0xfffffffc & addr);
}
static FileInfo *
make_file(str)
char *str;
{/* Create a file info structure */
extern APTR Lock();
APTR lock;
FileInfo *this;
char *tname;
struct FileInfoBlock *finfo;
finfo = (struct FileInfoBlock *)get_addr(&infodata);
tname = (char *)calloc(strlen(str)+1,1);
this = (FileInfo *)calloc(sizeof(FileInfo),1);
if(this==NULL || tname==NULL)
{printf("Out of memory\n");
exit(10);
}
strcpy(tname,str);
this->name = tname;
this->type = type_of(str);
/* Read the file date at this point */
lock = Lock(str,ACCESS_READ);
if(lock!=0)
{/* Got the lock now find the file info */
if(Examine(lock,finfo))
{
this->date = dstodate(&(finfo->fib_Date));
}
UnLock(lock);
}
return(this);
}
static Variable *
create_var(str)
char *str;
{/* Create a variable slot */
Variable *temp;
char *tname;
temp=(Variable *)calloc(sizeof(Variable),1);
tname=(char *)malloc(strlen(str)+1);
if(temp==NULL || tname==NULL)
{printf("Out of memory\n");
exit(10);
}
strcpy(tname,str);
temp->var = tname;
return(temp);
}
Variable *
find_var(str,create)
char *str;
int create;
{/* Find a variable definition */
Variable *ret_val;
Variable *here;
int hash_val;
hash_val = hash(str);
if(vars[hash_val]==NULL)
{/* Create new variable? */
if(create)
{
ret_val = create_var(str);
vars[hash_val] = ret_val;
return(ret_val);
}
else
return(NULL);
}
else
{/* We must wander the list looking for our man */
int found;
found = 0;
here = vars[hash_val];
while(here->next && (found = strcmp(here->var,str))!=0)
here = here->next;
if(found)
return(here);
else if (create)
{
ret_val = create_var(str);
here->next = ret_val;
return(ret_val);
}
else
return(NULL);
}
}
FileInfo *
find_file(str,create)
char *str;
int create;
{/* Find a file structure in the hash table */
FileInfo *ret_val;
ConsCell *here;
int hash_val;
hash_val = hash(str);
if(files[hash_val]==NULL)
{/* Empty slot, should we create an entry */
if(create)
{
ret_val = make_file(str);
here = cons(ret_val,NULL);
files[hash_val] = here;
return(ret_val);
}
else
return(NULL);
}
else
{/* We must wander the list looking for our man */
int found;
found = 0;
here = files[hash_val];
while((found = strcmp(here->car->name,str))!=0 && here->cdr)
here = here->cdr;
if(found==0)
return(here->car);
else if (create)
{
ret_val = make_file(str);
here->cdr = cons(ret_val,NULL);
return(ret_val);
}
else
return(NULL);
}
}
print_file(file)
FileInfo *file;
{/* Print out the details of a file */
ConsCell *this;
printf("File \"%s\"\n",file->name);
this = file->depends;
if(this)
{int i = 0;
printf(" Depends\n ");
while(this)
{printf("%s ",this->car->name);
i++;
if(i>3)
{printf("\n ");
i = 0;
}
this = this->cdr;
}
printf("\n");
}
else
printf(" No depends\n");
printf(" Create = ");
print_str(file->create);
}
print_files()
{/* Print out all the files in the hash table */
int i;
ConsCell *this;
for(i=0;i<MAX_HASH;i++)
{this = files[i];
while(this)
{print_file(this->car);
printf("\n");
this = this->cdr;
}
}
}
print_str(str)
String *str;
{/* Print out the string */
if(str==NULL)
printf("<NULL>\n");
else
{do {
printf("\"%s\"\n",str->contents);
str = str->next;
if(str)
printf(" ");
} while (str);
}
}
print_var(variable)
Variable *variable;
{/* Print out a single variable */
String *value;
printf("|%s| = ",variable->var);
value = variable->val;
print_str(variable->val);
}
print_vars()
{/* Print out all the variables and values */
int i;
Variable *this;
for(i=0;i<MAX_HASH;i++)
{this = vars[i];
while(this)
{print_var(this);
this = this->next;
}
}
}
print_def()
{/* Print the items on the defaults list */
ConsCell *this;
this = defaults;
while(this)
{print_file(this->car);
this = this->cdr;
}
}